package com.woniuxy.principal.service.impl;

import com.woniuxy.commons.entity.wyh.GradeInfo;
import com.woniuxy.commons.entity.wyh.TeacherInfo;
import com.woniuxy.principal.mapper.GradeDeanDao;
import com.woniuxy.principal.service.GradeDeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author： 雷鹏
 * Date: 2020/7/28 15:51
 */
@Service
public class GradeDeanServiceImpl implements GradeDeanService {

    @Autowired
    private GradeDeanDao gradeDeanDao;

    @Override
    public List<TeacherInfo> getAllTeacher(int sid) {
        return gradeDeanDao.getAllTeacher(sid);
    }

    @Override
    public List<GradeInfo> getAllGrade(int sid) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        String year = sdf.format(date);

        return gradeDeanDao.getAllGrade(sid, year);

    }

    @Override
    public void setGradeDean(int gaId, int tcId) {

        //根据年级id查询出对数据
        GradeInfo gradeDean = gradeDeanDao.getGradeDean(gaId);
        System.out.println(gradeDean);
        if (gradeDean.getGdId() != null && gradeDean.getGdId() != 0) {
            //更新年级主任
            gradeDeanDao.updateGradeDean(gradeDean.getGdId(), tcId);
        } else {
            //先插入年级主任，再更新到年级表
            gradeDeanDao.insertGradeDean(tcId);

            int max = gradeDeanDao.getMaxGradeDean();

            gradeDeanDao.setGradeDean(gaId, max);
        }

    }
}
